#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "core.h"

#define PRIMECOMMANDLENGTH 8

int main()
{
    system("title Book Manager");
    int i;
    printf("初始化...");
    initialize();
    char command_buf[PRIMECOMMANDLENGTH];
    printf("完成！\n");
    while(1)
    {
        clean_screen();
        printf("退出：exit\n");
        printf("请选择登陆类型，管理员(admin)/用户(user)：");
        scanf("%s", &command_buf);
        str_tolower(command_buf);
        fflush(stdin);
        if(strcmp(command_buf, "admin") == 0)
        {
            clean_screen();
            if(admin_login() != 1)
            {
                pause();
                continue;
            }
            clean_screen();
            while(1)
            {
                printf("Book Manager>");
                scanf("%s", &command_buf);
                fflush(stdin); //清空 stdin 缓存区
                str_tolower(command_buf);
                if(strcmp(command_buf, "add") == 0)
                    add();
                else if(strcmp(command_buf, "remove") == 0)
                    remove_lt();
                else if(strcmp(command_buf, "clean") == 0)
                    clean();
                else if(strcmp(command_buf, "show") == 0)
                    show();
                else if(strcmp(command_buf, "search") == 0)
                    search();
                else if(strcmp(command_buf, "borrow") == 0)
                    borrow();
                else if(strcmp(command_buf, "return") == 0)
                    return_lt();
                else if(strcmp(command_buf, "change") == 0)
                    change_info();
                else if(strcmp(command_buf, "overview") == 0)
                    overview();
                else if(strcmp(command_buf, "password") == 0)
                    change_admin_password();
                else if(strcmp(command_buf, "sync") == 0)
                    sync();
                else if(strcmp(command_buf, "exit") == 0)
                {
                    sync();
                    break;
                }
                else if(strcmp(command_buf, "help") == 0)
                    help();
                else
                {
                    printf("未找到命令 %s,输入 \"help\" 查看帮助。\n", command_buf);
                }
                for(i = 0; i < PRIMECOMMANDLENGTH; ++i)
                    command_buf[i] = 0;
            }
        }
        else if(strcmp(command_buf, "user") == 0)
        {
            clean_screen();
            if(user_login() != 1)
            {
                pause();
                continue;
            }
            clean_screen();
            while(1)
            {
                clean_screen();
                show_tip();
                int command_num = 0;
                scanf("%d", &command_num);
                fflush(stdin);
                if(command_num == 1)
                {
                    clean_screen();
                    interface_borrow();
                    pause();
                }
                else if(command_num == 2)
                {
                    clean_screen();
                    interface_return();
                    pause();
                }
                else if(command_num == 3)
                {
                    clean_screen();
                    interface_show();
                    pause();
                }
                else if(command_num == 4)
                {
                    clean_screen();
                    get_book_info();
                    pause();
                }
                else if(command_num == 5)
                {
                    clean_screen();
                    change_password();
                    pause();
                }
                else if(command_num == 6)
                {
                    clean_screen();
                    show_info();
                    pause();
                }
                else if(command_num == 7)
                {
                    user_logout();
                    sync();
                    break;
                }
                else
                {
                    printf("未找到命令 %d\n", command_num);
                    pause();
                }
            }
        }
        else if(strcmp(command_buf, "exit") == 0)
        {
            exit(0);
        }
        else
        {
            printf("未找到用户类型或命令 %s，请重新输入", command_buf);
            pause();
        }
        for(i = 0; i < PRIMECOMMANDLENGTH; ++i)
            command_buf[i] = 0;
    }
    return 0;
}
